package com.sj.purchase.modules.cost.config.task.synchronizedata;

import com.sj.purchase.modules.cost.config.task.TaskManager;
import com.sj.purchase.modules.cost.event.RunTaskEvent;
import com.sj.purchase.modules.cost.publisher.CustomPublisher;
import com.sj.purchase.modules.cost.domain.SucTask;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 同步数据
 * Created by MaJinGe on 17-11-09.
 */
@Component
public class SyncTaskManager implements TaskManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(SyncTaskManager.class);

    private CustomPublisher publisher;

    private StockTask2 stockTask2;

    private OrderTask2 orderTask2;

    private AdjustOrderTask2 adjustOrderTask2;

    private InventoryTask inventoryTask;

    public SyncTaskManager(CustomPublisher publisher, StockTask2 stockTask2, OrderTask2 orderTask2, AdjustOrderTask2 adjustOrderTask2, InventoryTask inventoryTask) {
        this.publisher = publisher;
        this.stockTask2 = stockTask2;
        this.orderTask2 = orderTask2;
        this.adjustOrderTask2 = adjustOrderTask2;
        this.inventoryTask = inventoryTask;
    }

    @Override
    public void runTask(SucTask.TaskType taskType, Date taskDay) {
        String taskDayStr = DateFormatUtils.format(taskDay, "yyyy/MM/dd");
        switch (taskType) {
            case SYNC_STOCK:
                LOGGER.info("[{}] :=> 开始同步库存", taskDayStr);
                boolean syncSuccess = stockTask2.synchronizeWmsStock();
                if(syncSuccess){
                    publishEvent(new RunTaskEvent(this, taskType, taskDay));
                }
                break;
            case SYNC_IN_ORDER:
                LOGGER.info("[{}] :=> 开始同步入库单", taskDayStr);
                orderTask2.synchronizeInOrder(taskDayStr);
                publishEvent(new RunTaskEvent(this, taskType, taskDay));
                break;
            case SYNC_OUT_ORDER:
                LOGGER.info("[{}] :=> 开始同步出库单", taskDayStr);
                orderTask2.synchronizeOutOrder(taskDayStr);
                publishEvent(new RunTaskEvent(this, taskType, taskDay));
                break;
            case SYNC_ADJUSTMENT:
                LOGGER.info("[{}] :=> 开始同步调整单", taskDayStr);
                adjustOrderTask2.synchronizeAdjustOrder(taskDayStr);
                publishEvent(new RunTaskEvent(this, taskType, taskDay));
                break;
            case SYNC_INVENTORY:
                LOGGER.info("[{}] :=> 开始同步库存明细", taskDayStr);
                inventoryTask.synchronizeInventory(taskDayStr);
                publishEvent(new RunTaskEvent(this, taskType, taskDay));
                break;
            default:
                throw new RuntimeException("");
        }
    }

    private void publishEvent(ApplicationEvent applicationEvent) {
        publisher.publishEvent(applicationEvent);
    }
}
